package com.sos.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.time.LocalDateTime;

/**
 * SOS帮助者关联表实体
 * 用于跟踪多个帮助者参与同一个SOS请求的情况
 */
@Data
@Entity
@Table(name = "sos_helper", 
       uniqueConstraints = {@UniqueConstraint(columnNames = {"sos_id", "helper_id"})})
@DynamicInsert
@DynamicUpdate
public class SosHelper {
    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * SOS请求ID（关联sos_request表）
     */
    @Column(name = "sos_id", nullable = false)
    private Long sosId;

    /**
     * 帮助者用户ID（关联user表）
     */
    @Column(name = "helper_id", nullable = false)
    private Long helperId;

    /**
     * 帮助者状态：
     * 0: 已邀请但未响应
     * 1: 已接受
     * 2: 已拒绝
     * 3: 已完成
     */
    @Column(nullable = false, columnDefinition = "tinyint default 0")
    private Integer status = 0;

    /**
     * 是否已读（0:未读,1:已读，默认0）
     */
    @Column(name = "is_read", nullable = false, columnDefinition = "tinyint default 0")
    private Integer isRead = 0;

    /**
     * 是否隐藏（0:显示,1:隐藏，默认0）
     */
    @Column(name = "is_hidden", nullable = false, columnDefinition = "tinyint default 0")
    private Integer isHidden = 0;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 关联SOS请求
     */
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sos_id", insertable = false, updatable = false)
    private SosRequest sosRequest;

    /**
     * 关联帮助者用户
     */
    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "helper_id", insertable = false, updatable = false)
    private User helper;

    @PrePersist
    public void prePersist() {
        this.createdTime = LocalDateTime.now();
        this.updatedTime = LocalDateTime.now();
    }

    @PreUpdate
    public void preUpdate() {
        this.updatedTime = LocalDateTime.now();
    }
}